﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using InnovTech.Wpf.Command;
using System.Windows;
using System.ComponentModel;
using System.Threading;
using System.Reactive.Linq;
using Dropbox.DataHouse.Logic;
using System.IO;
using InnovTech.Util.Zip;

namespace Dropbox.DataHouse.View
{
    public class MinIconWindowViewModel : INotifyPropertyChanged
    {

        public MinIconWindowViewModel()
        {
            Model = new DataHouseModel();
            CloseCmd = new DelegateCommand<Application>(app =>
            {
                app.Shutdown();
            });
            RestoreCmd = new DelegateCommand<Application>(app =>
            {
                foreach (Window win in app.Windows)
                {
                    win.Hide();
                }
                app.MainWindow.Show();
            });

            AddNewByDropCmd = new DelegateCommand<IDataObject>(
               data =>
               {

                   CurrentActionName = "Adding";
                   AnyActionRunning = true;
                   string[] FileList = (string[])data.GetData(DataFormats.FileDrop, false);
                   SynchronizationContext context = SynchronizationContext.Current;
                   Observable.Start(() =>
                       FileList.Select(file => 
                       {
                           bool isZippedDir = false;
                           string filePath = null;
                           if (File.Exists(file))
                               filePath = file;
                           else
                           {
                               VsCodeHandler.DeleteVsUnusfulDirs(file);
                               filePath = SharpZipUtil.CompressFolder(file, Directory.GetCurrentDirectory());
                               isZippedDir = true;
                           }
                           var fileInfo = Model.AddNewFile(filePath);
                           if (isZippedDir)
                               File.Delete(filePath);
                           return fileInfo;
                           //return Model.AddNewFile(null, filePath); 
                       }).
                       ToObservable()
                       //.ObserveOn(context)
                       .Subscribe(file => 
                       { 
                           //Application.Current.MainWindow.Dispatcher.Invoke(new Action(() =>{
                              
                           //}));
                       }, 
                       () => AnyActionRunning = false)
                   );
               });

        }
        public ICommand AddNewByDropCmd { get; set; }
        public ICommand CloseCmd { get; set; }
        public ICommand RestoreCmd { get; set; }
        DataHouseModel Model { get; set; }

        bool _anyActionRunning;

        public bool AnyActionRunning
        {
            get { return _anyActionRunning; }
            set
            {
                if (_anyActionRunning != value)
                {
                    _anyActionRunning = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("AnyActionRunning"));
                    }
                }
            }
        }

        string _currentActionName = "Syncronization dakfjdksf skd ";

        public string CurrentActionName
        {
            get { return _currentActionName; }
            set
            {
                if (_currentActionName != value)
                {
                    _currentActionName = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("CurrentActionName"));
                    }
                }
            }
        }

        string _currentActionState;

        public string CurrentActionState
        {
            get { return _currentActionState; }
            set
            {
                if (_currentActionState != value)
                {
                    _currentActionState = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("CurrentActionState"));
                    }
                }
            }
        }

        float _currentActionPercentageProgress;
        public float CurrentActionPercentageProgress
        {
            get { return _currentActionPercentageProgress; }
            set
            {
                if (_currentActionPercentageProgress != value)
                {
                    _currentActionPercentageProgress = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("CurrentActionPercentageProgress"));
                    }
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
